#!/usr/bin/env python
# -*- coding:utf-8 -*-
#/home/zc/workspace/tinyfeet/trunk/src/controllers/weibo2tfeet/tfeet_from_weibo.py
'''
定时获取@tinyfeet的微博，将其转为tfeet发送到tinyfeet.com。
每2分钟检查一次。
每小时30次。

根据最近一次成功保存@的微博ID获取更新的微博ID

获取@我的微博后放到队列异步处理，防止堵塞下次获取微博
队列由redis提供服务。

Created on 2012-3-28

@author: zc
'''
from controllers.weibo.weibo import APIClient
from config.settings import CONSUME_KEY, CONSUME_SECRET, REDIS_SRV_HOST, REDIS_SRV_PORT, _CALLBACK_URL, QUEUE_USER, QUEUE_TFEET, QUEUE_NONE_COORDINATE
import web
from controllers.sessionHandler import session_get
import redis
from config.sqlmap import SQL_GET_SINCE_ID_WEIBO
from dbhandle.mysqldb import mysqldb 

queue = redis.Redis( host = REDIS_SRV_HOST, port = REDIS_SRV_PORT )
      
class tfeet_from_weibo:
    def GET( self ):
        self.access_token, self.uid, self.expires_in = session_get( web.config._session )
        self.client = APIClient( app_key = CONSUME_KEY, app_secret = CONSUME_SECRET )
        if self.access_token is None:
            raise web.found( self.client.get_authorize_url( _CALLBACK_URL ) )
        self.client.set_access_token( self.access_token, self.expires_in )

        #since_id = queue.get( 'since_id' ) if queue.get( 'since_id' )  is not None else self.get_since_id()
        self.next_cursor = 1
        self.page = 1
        since_id = 3430724617111275
        self.res = self.client.get.statuses__mentions( since_id = since_id, page = self.page, filter_by_type = 1 )
        
        while self.next_cursor > 0:
            self.res = self.client.get.statuses__mentions( since_id = since_id, page = self.page, filter_by_type = 1 )
            self.next_cursor = self.res.next_cursor
            self.page = self.page + 1
            if len( self.res.statuses ) == 0:
                break
            self.set_queue( self.res )

        return   self.res 


    def get_since_id( self ):
        dbo = mysqldb()
        since_id = 0
        try:
            res = dbo.query( SQL_GET_SINCE_ID_WEIBO )
            if res:
                since_id = res[0].since_id 
        except:
            since_id = 0
        return since_id
    
    
    def set_queue( self, res ):
        for tf in res.statuses:
            user = tf.user #get userinfo, a jsonObject
            longtitude, latitude = None
            
            if tf.geo is None: #没有坐标信息，存入待处理队列，继续循环，将对其微博发布一条评论，告知没有坐标信息无法转入tfeet
                queue.rpush( QUEUE_NONE_COORDINATE, tf )
                continue
            
            longtitude = tf.geo.coordinates[0]  # coordinate 经纬度坐标 逗号分割  'coordinates': [32.054363, 118.767036]}
            latitude = tf.geo.coordinates[1]
            tfeet = {
                        u'tfid' : None,
                        u'uid' : user.id,
                        u'text' : tf.text,
                        u'pic_normal' : tf.bmiddle_pic,
                        u'pic_small' : tf.thumbnail_pic,
                        u'pic_original' : tf.original_pic ,
                        u'long' : longtitude,
                        u'lat' : latitude,
                        u'location' :  None, #根据long,lat坐标获取location
                        u'weibo_id': tf.id ,
                        u'source':'weibo.com'
                        }
            queue.rpush( QUEUE_USER, user )
            queue.rpush( QUEUE_TFEET, tfeet )
            
